/*
 * Name: cmd-tool.c
 * Project: elektor HiD Temperature Modul
 * Author: Michael Odenwald
 * Creation Date: 10.05.2013
 */
 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include "hiddata.h"
#include "../firmware/usbconfig.h"  /* for device VID, PID, vendor name and product name */

/*
 * usbErrorMessage() print out USB error Messages
 */
static char *usbErrorMessage(int errCode)
{
	static char buffer[80];

    switch(errCode){
        case USBOPEN_ERR_ACCESS:      return "Access to device denied";
        case USBOPEN_ERR_NOTFOUND:    return "The specified device was not found";
        case USBOPEN_ERR_IO:          return "Communication error with device";
        default:
            sprintf(buffer, "Unknown USB error %d", errCode);
            return buffer;
    }
    return NULL;    /* not reached */
}

/*
 * openDevice() open the USB HiD device with the given Vendor ID and Product ID
 */
static usbDevice_t  *openDevice(void)
{
	usbDevice_t     *dev = NULL;
	unsigned char   rawVid[2] = {USB_CFG_VENDOR_ID}, rawPid[2] = {USB_CFG_DEVICE_ID};
	char            vendorName[] = {USB_CFG_VENDOR_NAME, 0}, productName[] = {USB_CFG_DEVICE_NAME, 0};
	int             vid = rawVid[0] + 256 * rawVid[1];
	int             pid = rawPid[0] + 256 * rawPid[1];
	int             err;

    if((err = usbhidOpenDevice(&dev, vid, vendorName, pid, productName, 0)) != 0)
	{
        fprintf(stderr, "error finding %s: %s\n", productName, usbErrorMessage(err));
        return NULL;
    }
    return dev;
}

/*
 * usage() how the program works
 */
static void usage(char *myName)
{
    fprintf(stderr, "usage:\n");
	fprintf(stderr, "  %s tval\n", myName);
	fprintf(stderr, "  %s version\n", myName);
}

/*
 * main()
 *
 * Start of the elektor HiD Temperature Modul firmware cmd-tool
 */
int main(int argc, char **argv)
{
	usbDevice_t 	*dev;
	int				err;
	int				len;
	char			buffer[129];    /* space for dummy report ID */
	int32_t			tval;

    if(argc < 2)
	{
        usage(argv[0]);
        exit(1);
    }
    
	if((dev = openDevice()) == NULL)
	{
        exit(1);
	}

	if(strcasecmp(argv[1], "tval") == 0)
	{
		len = 5;

		memset(buffer, 0, sizeof(buffer));
		
        if((err = usbhidGetReport(dev, 10, buffer, &len)) != 0) /* read temperatur sensor with ReportID 10 */
		{ 
            fprintf(stderr, "error reading data: %s\n", usbErrorMessage(err));	
		}
		else
		{
			tval = (unsigned long)(buffer[2] << 24) | (buffer[3] << 16) | (buffer[4] << 8) | buffer[5];	
			fprintf(stderr, "temperature is: %+.4f\n", (float)tval/10000.0);
		}
	}			
	else if(strcasecmp(argv[1], "version") == 0)
	{
        len = 12;
		char out[12];
		
		memset (buffer, 0, sizeof(buffer));
		memset (out, 0, sizeof(out));
		
        if((err = usbhidGetReport(dev, 20, buffer, &len)) != 0) /* read version information with ReportID 20 */
		{ 
            fprintf(stderr, "error reading data: %s\n", usbErrorMessage(err));	
		}
		else
		{
			memcpy(out, &buffer[2], 10);
			fprintf(stderr, "UsbDeviceVersion: %s\n", out);
		}
	}	
	else	
	{
        usage(argv[0]);
        exit(1);
    }
    usbhidCloseDevice(dev);
    return 0;
}

